package hibernate.method;

import hibernate.method.interfaces.FeedbackMethodsInterface;
import hibernate.model.EventFeedback;
import hibernate.model.GameFeedback;
import hibernate.model.LocalFeedback;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class FeedbackMethods implements FeedbackMethodsInterface {

	@Override
	public void addLocalFeedback(SessionFactory sessionFactory,
			Session session, String user_id, int local_id, String comment) {
		LocalFeedback fb = new LocalFeedback();
		if (user_id != null)
			fb.setUser_id(user_id);
			else
				fb.setUser_id(null);
		fb.setAdd_date(AdditionalMethods.getCurrentDate());
		fb.setLocal_id(local_id);
		fb.setComment(comment);
		session.save(fb);
	}

	public void addGameFeedback(SessionFactory sessionFactory, Session session,
			String user_id, int game_id, String comment) {
		GameFeedback fb = new GameFeedback();
		if (user_id != null)
		fb.setUser_id(user_id);
		else
			fb.setUser_id(null);
		fb.setAdd_date(AdditionalMethods.getCurrentDate());
		fb.setGame_id(game_id);
		fb.setComment(comment);
		session.save(fb);
	}

	@Override
	public void deleteLocalFeedback(SessionFactory sessionFactory,
			Session session, LocalFeedback fb) {
		session.delete(fb);

	}

	@Override
	public void deleteGameFeedback(SessionFactory sessionFactory,
			Session session, GameFeedback gb) {
		session.delete(gb);

	}

	@Override
	public GameFeedback findGameFeebackById(SessionFactory sessionFactory,
			Session session, int feedback_id) {
		GameFeedback gamefb = (GameFeedback) session.get(GameFeedback.class,
				new Integer(feedback_id));
		return gamefb;
	}

	@Override
	public LocalFeedback findLocalFeebackById(SessionFactory sessionFactory,
			Session session, int feedback_id) {
		LocalFeedback localfb = (LocalFeedback) session.get(
				LocalFeedback.class, new Integer(feedback_id));
		return localfb;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GameFeedback> listGameFeedbacks(SessionFactory sessionFactory,
			Session session, int game_id) {
		Criteria cr = session.createCriteria(GameFeedback.class);
		Criterion game = Restrictions.eq("game_id", game_id);
		cr.add(game);
		return cr.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LocalFeedback> listLocalFeedbacks(
			SessionFactory sessionFactory, Session session, int local_id) {
		Criteria cr = session.createCriteria(LocalFeedback.class);
		Criterion local = Restrictions.eq("local_id", local_id);
		cr.add(local);
		return cr.list();
	}

	@Override
	public int countFbByLocalId(SessionFactory sessionFactory, Session session,
			int local_id) {
		Criteria cr = session.createCriteria(LocalFeedback.class);
		cr.add(Restrictions.eq("local_id", local_id));
		return ((Number)cr.setProjection(Projections.rowCount()).uniqueResult()).intValue();
	}

	@Override
	public int countFbByGameId(SessionFactory sessionFactory, Session session,
			int game_id) {
		Criteria cr = session.createCriteria(GameFeedback.class);
		cr.add(Restrictions.eq("game_id", game_id));
		return ((Number)cr.setProjection(Projections.rowCount()).uniqueResult()).intValue();
	}

	@Override
	public GameFeedback idtoGameFeedback(SessionFactory sessionFactory,
			Session session, int gameFeedback) {
		GameFeedback gfb = (GameFeedback) session.get(GameFeedback.class, new Integer(gameFeedback));
		return gfb;
	}

	@Override
	public LocalFeedback idToLocalFeedback(SessionFactory sessionFactory,
			Session session, int localFeedback) {
		LocalFeedback lfb = (LocalFeedback) session.get(LocalFeedback.class, new Integer(localFeedback));
		return lfb;
	}

	@Override
	public void addEventFeedback(SessionFactory sessionFactory,
			Session session, String user_id, int event_id, String comment) {
		EventFeedback fb = new EventFeedback();
			fb.setUser_id(user_id);
		fb.setAdd_date(AdditionalMethods.getCurrentDate());
		fb.setEvent_id(event_id);
		fb.setComment(comment);
		session.save(fb);
		
	}

	@Override
	public void deleteEventFeedback(SessionFactory sessionFactory,
			Session session, EventFeedback efb) {
		session.delete(efb);
		
	}

	@Override
	public EventFeedback idToEventFeedback(SessionFactory sessionFactory,
			Session session, int eventFeedback) {
		EventFeedback lfb = (EventFeedback) session.get(EventFeedback.class, new Integer(eventFeedback));
		return lfb;
	}

	@Override
	public EventFeedback findEventFeebackById(SessionFactory sessionFactory,
			Session session, int eventfb_id) {
		EventFeedback eventfb = (EventFeedback) session.get(
				EventFeedback.class, new Integer(eventfb_id));
		return eventfb;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EventFeedback> listEventFeedbacks(
			SessionFactory sessionFactory, Session session, int event_id) {
		Criteria cr = session.createCriteria(EventFeedback.class);
		Criterion local = Restrictions.eq("event_id", event_id);
		cr.add(local);
		return cr.list();
	}
	
	

}
